/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.packager.gui.frames;

import java.awt.Color;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;

import sshtunneler.library.ExtensionFilter;
import sshtunneler.packager.data.Config;

public class ShowPaths extends MyInternalFrame {
	private static final long serialVersionUID = 984044762125508441L;

	public ShowPaths(MyInternalFrameActionDispatcherFrame dispatcher) {
		super(dispatcher);
		this.initComponents();
		dispatcher.add_path_tools_listener(this);
		dispatcher.add_path_tunneler_listener(this);
		dispatcher.add_path_installer_listener(this);
		this.refresh();
	}

	private void initComponents() {

		this.jPanel1 = new javax.swing.JPanel();
		this.jLabel1 = new javax.swing.JLabel();
		this.jPanel4 = new javax.swing.JPanel();
		this.txt_tunneler = new javax.swing.JTextField();
		this.cmd_browse_tunneler = new javax.swing.JButton();
		this.jPanel2 = new javax.swing.JPanel();
		this.jLabel2 = new javax.swing.JLabel();
		this.jPanel5 = new javax.swing.JPanel();
		this.txt_installer = new javax.swing.JTextField();
		this.cmd_browse_installer = new javax.swing.JButton();
		this.jPanel3 = new javax.swing.JPanel();
		this.jLabel3 = new javax.swing.JLabel();
		this.jPanel6 = new javax.swing.JPanel();
		this.txt_tools = new javax.swing.JTextField();
		this.cmd_browse_tools = new javax.swing.JButton();

		this.setClosable(true);
		this.setResizable(true);
		this.setTitle("Paths");
		this.setMaximumSize(new java.awt.Dimension(600, 220));
		this.setMinimumSize(new java.awt.Dimension(200, 220));
		this.setName("Form");
		this.setPreferredSize(new java.awt.Dimension(250, 220));
		this.getContentPane().setLayout(
				new javax.swing.BoxLayout(this.getContentPane(),
						javax.swing.BoxLayout.Y_AXIS));

		this.jPanel1.setMaximumSize(new java.awt.Dimension(1000, 60));
		this.jPanel1.setMinimumSize(new java.awt.Dimension(200, 60));
		this.jPanel1.setName("jPanel1");
		this.jPanel1.setPreferredSize(new java.awt.Dimension(250, 60));
		this.jPanel1.setLayout(new java.awt.BorderLayout(10, 5));

		this.jLabel1.setText("  SSHTunneler");
		this.jLabel1.setMaximumSize(new java.awt.Dimension(67, 8));
		this.jLabel1.setMinimumSize(new java.awt.Dimension(67, 8));
		this.jLabel1.setName("jLabel1");
		this.jLabel1.setPreferredSize(new java.awt.Dimension(67, 20));
		this.jPanel1.add(this.jLabel1, java.awt.BorderLayout.NORTH);

		this.jPanel4.setMaximumSize(new java.awt.Dimension(1000, 15));
		this.jPanel4.setMinimumSize(new java.awt.Dimension(200, 15));
		this.jPanel4.setName("jPanel4");
		this.jPanel4.setPreferredSize(new java.awt.Dimension(200, 15));
		this.jPanel4.setLayout(new javax.swing.BoxLayout(this.jPanel4,
				javax.swing.BoxLayout.LINE_AXIS));

		this.txt_tunneler.setEditable(false);
		this.txt_tunneler.setMaximumSize(new java.awt.Dimension(1000, 20));
		this.txt_tunneler.setMinimumSize(new java.awt.Dimension(100, 20));
		this.txt_tunneler.setName("txt_tunneler");
		this.txt_tunneler.setPreferredSize(new java.awt.Dimension(100, 20));
		this.jPanel4.add(this.txt_tunneler);

		this.cmd_browse_tunneler.setText("...");
		this.cmd_browse_tunneler.setName("cmd_browse_tunneler");
		this.cmd_browse_tunneler
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						ShowPaths.this.cmd_browse_tunnelerActionPerformed(evt);
					}
				});
		this.jPanel4.add(this.cmd_browse_tunneler);

		this.jPanel1.add(this.jPanel4, java.awt.BorderLayout.CENTER);

		this.getContentPane().add(this.jPanel1);

		this.jPanel2.setMaximumSize(new java.awt.Dimension(1000, 60));
		this.jPanel2.setMinimumSize(new java.awt.Dimension(200, 60));
		this.jPanel2.setName("jPanel2");
		this.jPanel2.setPreferredSize(new java.awt.Dimension(250, 60));
		this.jPanel2.setLayout(new java.awt.BorderLayout(10, 5));

		this.jLabel2.setText("  SSHInstaller");
		this.jLabel2.setName("jLabel2");
		this.jPanel2.add(this.jLabel2, java.awt.BorderLayout.NORTH);

		this.jPanel5.setMaximumSize(new java.awt.Dimension(1000, 15));
		this.jPanel5.setMinimumSize(new java.awt.Dimension(200, 15));
		this.jPanel5.setName("jPanel5");
		this.jPanel5.setPreferredSize(new java.awt.Dimension(200, 15));
		this.jPanel5.setLayout(new javax.swing.BoxLayout(this.jPanel5,
				javax.swing.BoxLayout.LINE_AXIS));

		this.txt_installer.setEditable(false);
		this.txt_installer.setMaximumSize(new java.awt.Dimension(1000, 20));
		this.txt_installer.setMinimumSize(new java.awt.Dimension(100, 20));
		this.txt_installer.setName("txt_installer");
		this.txt_installer.setPreferredSize(new java.awt.Dimension(100, 20));
		this.jPanel5.add(this.txt_installer);

		this.cmd_browse_installer.setText("...");
		this.cmd_browse_installer.setName("cmd_browse_installer");
		this.cmd_browse_installer
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						ShowPaths.this.cmd_browse_installerActionPerformed(evt);
					}
				});
		this.jPanel5.add(this.cmd_browse_installer);

		this.jPanel2.add(this.jPanel5, java.awt.BorderLayout.CENTER);

		this.getContentPane().add(this.jPanel2);

		this.jPanel3.setMaximumSize(new java.awt.Dimension(1000, 60));
		this.jPanel3.setMinimumSize(new java.awt.Dimension(200, 60));
		this.jPanel3.setName("jPanel3");
		this.jPanel3.setPreferredSize(new java.awt.Dimension(250, 60));
		this.jPanel3.setLayout(new java.awt.BorderLayout(10, 5));

		this.jLabel3.setText("  Tools");
		this.jLabel3.setName("jLabel3");
		this.jPanel3.add(this.jLabel3, java.awt.BorderLayout.NORTH);

		this.jPanel6.setMaximumSize(new java.awt.Dimension(1000, 15));
		this.jPanel6.setMinimumSize(new java.awt.Dimension(200, 15));
		this.jPanel6.setName("jPanel6");
		this.jPanel6.setPreferredSize(new java.awt.Dimension(200, 15));
		this.jPanel6.setLayout(new javax.swing.BoxLayout(this.jPanel6,
				javax.swing.BoxLayout.LINE_AXIS));

		this.txt_tools.setEditable(false);
		this.txt_tools.setMaximumSize(new java.awt.Dimension(1000, 20));
		this.txt_tools.setMinimumSize(new java.awt.Dimension(100, 20));
		this.txt_tools.setName("txt_tools");
		this.txt_tools.setPreferredSize(new java.awt.Dimension(100, 20));
		this.jPanel6.add(this.txt_tools);

		this.cmd_browse_tools.setText("...");
		this.cmd_browse_tools.setName("cmd_browse_tools");
		this.cmd_browse_tools
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						ShowPaths.this.cmd_browse_toolsActionPerformed(evt);
					}
				});
		this.jPanel6.add(this.cmd_browse_tools);

		this.jPanel3.add(this.jPanel6, java.awt.BorderLayout.CENTER);

		this.getContentPane().add(this.jPanel3);

		this.pack();
	}

	public void refresh() {
		this.txt_installer.setText(Config.inst.installerdir_path);
		this.txt_tunneler.setText(Config.inst.tunnelerdir_path);
		this.txt_tools.setText(Config.inst.toolsdir_path);
		this.check_paths();
	}

	private void check_paths() {
		if (!Config.inst.validate_installer()) {
			this.txt_installer.setBorder(BorderFactory
					.createLineBorder(Color.RED));
		} else {
			this.txt_installer.setBorder(BorderFactory
					.createLineBorder(Color.GREEN));
		}
		if (!Config.inst.validate_tunneler()) {
			this.txt_tunneler.setBorder(BorderFactory
					.createLineBorder(Color.RED));
		} else {
			this.txt_tunneler.setBorder(BorderFactory
					.createLineBorder(Color.GREEN));
		}
		if (!Config.inst.validate_tools_directory()) {
			this.txt_tools.setBorder(BorderFactory.createLineBorder(Color.RED));
		} else {
			this.txt_tools.setBorder(BorderFactory
					.createLineBorder(Color.GREEN));
		}
	}

	private void cmd_browse_tunnelerActionPerformed(
			java.awt.event.ActionEvent evt) {
		JFileChooser chooser = new JFileChooser();
		chooser.setMultiSelectionEnabled(false);
		ExtensionFilter filter = new ExtensionFilter("Java executable file");
		filter.add_accepted_extension(".jar");
		chooser.setFileFilter(filter);
		chooser.setCurrentDirectory(new File("."));

		chooser.showOpenDialog(null);

		File selection = chooser.getSelectedFile();
		if (selection == null) {
			return;
		}

		String path = selection.getAbsolutePath();
		path = path.substring(0, path.lastIndexOf(File.separator));
		path = path + File.separator;

		if (!path.equals(Config.inst.tunnelerdir_path)) {
			Config.inst.tunnelerdir_path = path;
			this.dispatcher.fire_path_tools_changed();
		}
	}

	private void cmd_browse_installerActionPerformed(
			java.awt.event.ActionEvent evt) {
		JFileChooser chooser = new JFileChooser();
		chooser.setMultiSelectionEnabled(false);
		ExtensionFilter filter = new ExtensionFilter("Java executable file");
		filter.add_accepted_extension(".jar");
		chooser.setCurrentDirectory(new File("."));
		chooser.setFileFilter(filter);

		chooser.showOpenDialog(null);

		File selection = chooser.getSelectedFile();
		if (selection == null) {
			return;
		}

		String path = selection.getAbsolutePath();
		path = path.substring(0, path.lastIndexOf(File.separator));
		path = path + File.separator;

		if (!path.equals(Config.inst.installerdir_path)) {
			Config.inst.installerdir_path = path;
			this.dispatcher.fire_path_tools_changed();
		}
	}

	private void cmd_browse_toolsActionPerformed(java.awt.event.ActionEvent evt) {
		JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		chooser.setMultiSelectionEnabled(false);
		chooser.setCurrentDirectory(new File("."));

		chooser.showOpenDialog(null);

		File selection = chooser.getSelectedFile();
		if (selection == null) {
			return;
		}

		String path = selection.getAbsolutePath();
		if (!path.endsWith(File.separator)) {
			path = path + File.separator;
		}

		if (!path.equals(Config.inst.toolsdir_path)) {
			Config.inst.toolsdir_path = path;
			this.dispatcher.fire_path_tools_changed();
		}
	}

	private javax.swing.JButton cmd_browse_installer;
	private javax.swing.JButton cmd_browse_tools;
	private javax.swing.JButton cmd_browse_tunneler;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JPanel jPanel4;
	private javax.swing.JPanel jPanel5;
	private javax.swing.JPanel jPanel6;
	private javax.swing.JTextField txt_installer;
	private javax.swing.JTextField txt_tools;
	private javax.swing.JTextField txt_tunneler;

}
